package com.hst.web.base.proxy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.hst.core.MapBean;
import com.hst.core.Page;
import com.hst.core.ServiceContext;
import com.hst.core.dao.IORDao;
import com.hst.core.dao.ISQLDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.meta.MetaData;
import com.hst.core.meta.proxies.DefaultDataProxy;
import com.hst.web.auth.po.User;
import com.hst.web.base.entity.TAccountIdentity;
import com.hst.web.base.entity.TAccountInfo;
import com.hst.web.base.entity.TCompanyEmployee;
import com.hst.web.base.ui.AccountInfo;

@Transactional
public class AccountInfoProxy extends DefaultDataProxy {

	@Autowired
	IORDao dao;

	@Autowired
	ISQLDao sqlDao;	
	
	@Override
	public Page<?> list(Map<String, ?> query, int pageIdx) {
		MapBean<String, Object> querys = new MapBean<>();
		for (String key : query.keySet()) {
			querys.put(key, query.get(key));
		}

		Page<?> page = sqlDao.list("account.get-account-list", querys, pageIdx, Page.getPageSize());
		return page;
	}

	@Override
	public List<?> list(Map<String, ?> query) {
		MapBean<String, Object> querys = new MapBean<>();
		for (String key : query.keySet()) {
			querys.put(key, query.get(key));
		}
		
		List<?> list = sqlDao.query("account.get-account-list", querys);
		return list;
	}

	@Override
	public Object save(MetaData data) throws Exception {
		String userName, papersType, papersNumber;
		TAccountInfo tAccountInfo;
		TAccountIdentity tAccountIdentity;
		
		AccountInfo accountInfo = (AccountInfo) data;
		userName = accountInfo.getUserName();
		papersType = accountInfo.getPapersType();
		papersNumber = accountInfo.getPapersNumber();
		
		tAccountInfo = dao.get(TAccountInfo.class, userName);
		tAccountIdentity = dao.get(TAccountIdentity.class, userName);
		//保存账户信息表(TAccountInfo)和账户实名身份信息表(t_account_identity)
		if (1 == data.getOp()) {
			//验证用户名是否存在
			if (tAccountInfo != null || tAccountIdentity != null) {
				throw new Exception("该用户名已存在！");
			}
			
			//验证证件号重复
			List<ORQuery> querys = new ArrayList<ORQuery>();
			querys.add(new ORQuery(Op.eq, "papersType", papersType));
			querys.add(new ORQuery(Op.eq, "papersNumber", papersNumber));
			List<TAccountIdentity> accountIdentityList= dao.list(TAccountIdentity.class, querys);
			if (accountIdentityList.size() > 0) {
				throw new Exception("该证件号已存在！");
			}
			
			tAccountInfo = new TAccountInfo();
			BeanUtils.copyProperties(tAccountInfo, data);
			dao.save(tAccountInfo);
			
			tAccountIdentity = new TAccountIdentity();
			BeanUtils.copyProperties(tAccountIdentity, data);
			dao.save(tAccountIdentity);
		} else {
			//验证证件号重复
			List<ORQuery> querys = new ArrayList<ORQuery>();
			querys.add(new ORQuery(Op.eq, "papersType", papersType));
			querys.add(new ORQuery(Op.eq, "papersNumber", papersNumber));
			querys.add(new ORQuery(Op.neq, "userName", userName));
			List<TAccountIdentity> accountIdentityList= dao.list(TAccountIdentity.class, querys);
			if (accountIdentityList.size() > 0) {
				throw new Exception("该证件号已存在！");
			}
			
			tAccountInfo = dao.get(TAccountInfo.class, userName);
			BeanUtils.copyProperties(tAccountInfo, data);
			dao.update(tAccountInfo);
			
			tAccountIdentity = dao.get(TAccountIdentity.class, userName);
			if (tAccountIdentity != null) {
				BeanUtils.copyProperties(tAccountIdentity, data);
				dao.update(tAccountIdentity);
				
				String employeeCode = tAccountIdentity.getEmployeeCode();
				String fullname = tAccountIdentity.getFullname();
				if (StringUtils.isNotEmpty(employeeCode)) {
					TCompanyEmployee tCompanyEmployee = dao.get(TCompanyEmployee.class, employeeCode);
					tCompanyEmployee.setFullname(fullname);
					dao.update(tCompanyEmployee);
				}
			}

		}
		
		data.setOp(0);		//修改操作状态为更新
		return data;
	}
	
	@Override
	public Object del(MetaData data) throws Exception {
		TAccountInfo tAccountInfo;
		TAccountIdentity tAccountIdentity;
		
		AccountInfo accountInfo = (AccountInfo) data;
		String userName = accountInfo.getUserName();
		
		tAccountInfo = dao.get(TAccountInfo.class, userName);
		dao.delete(tAccountInfo);
		
		tAccountIdentity = dao.get(TAccountIdentity.class, userName);
		dao.delete(tAccountIdentity);
		
		return data;
	}
}
